package md.mru.core.service;

import md.mru.core.common.BaseDtoMapper;
import md.mru.core.domains.OrderPattern;
import md.mru.core.domains.SystemTag;
import md.mru.core.domains.administration.User;
import md.mru.core.domains.administration.UserGroup;
import md.mru.core.dtos.OrderPatternDto;
import md.mru.core.dtos.SystemTagDto;
import md.mru.core.dtos.UserDto;
import md.mru.core.dtos.UserGroupDto;
import md.mru.core.repositories.OrderPatternRepository;
import md.mru.core.repositories.SystemTagRepository;
import md.mru.core.repositories.UserGroupRepository;
import md.mru.core.repositories.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author sergiu.marin
 * @since 16.04.2014.
 */
@Service
public class AdministrationService {

    /*@Value("${first_header_div}")
    private String firstHeaderDiv;
    @Value("${second_header_div}")
    private String secondHeaderDiv;*/

    @Autowired
    @Qualifier(value = "dtoMapper")
    private BaseDtoMapper mapper;
    @Autowired
    private UserGroupRepository userGroupRepository;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private SystemTagRepository systemTagRepository;
    @Autowired
    private OrderPatternRepository orderPatternRepository;

    public List<UserGroupDto> getGroups() {
        List<UserGroup> groups = userGroupRepository.findAll();
        return mapper.mapList(groups, UserGroupDto.class);
    }

    public long saveUser(UserDto userDto) {
        User user = mapper.map(userDto, User.class);
        return userRepository.save(user).getId();
    }

    public void deleteUser(long userId) {
        userRepository.delete(userId);
    }

    public List<UserDto> getUsersByGroup(long groupId) {
        return null;
    }

    public List<UserDto> getAllUsers(long id) {
        List<User> users = userRepository.getAllUsers(id);
        return mapper.mapList(users, UserDto.class);
    }

    public long saveGroup(UserGroupDto userGroupDto) {
        UserGroup userGroup = mapper.map(userGroupDto, UserGroup.class);
        return userGroupRepository.save(userGroup).getId();

    }

    public void deleteUserGroup(long groupId) {
        userGroupRepository.delete(groupId);
    }

    public List<SystemTagDto> getSystemTags() {
        List<SystemTag> systemTags = systemTagRepository.findAll();
        return mapper.mapList(systemTags, SystemTagDto.class);
    }

    public void deleteSystemTag(long systemTagId) {
        systemTagRepository.delete(systemTagId);
    }

    public long saveSystemTag(SystemTagDto systemTagDto) {
        SystemTag systemTag = mapper.map(systemTagDto, SystemTag.class);
        return systemTagRepository.save(systemTag).getId();
    }

    /*@Transactional
    public void saveOrderPattern(OrderPatternDto orderPatternDto) {
        OrderPattern orderPattern = mapper.map(orderPatternDto, OrderPattern.class);
        orderPatternRepository.save(orderPattern);
    }

    @Transactional
    public void deleteOrderPattern(long orderPatternId) {
        orderPatternRepository.deleteOrderPattern(orderPatternId);
    }

    public String getOrderTemplateHeader() {
        return firstHeaderDiv + secondHeaderDiv;
    }*/

}
